<?php

use Lib\Dao\GestoresDao;
use Lib\Utils\Funciones;
use Lib\Dao\MenusDao;
use Lib\Utils\MenusUtils;
use Lib\Dao\LogsDao;
use Illuminate\Support\Facades\DB;

class GestoresController extends \BaseController {

	
	public function getAllGestores(){}
	
	public function getGestorPorId(){
		
		if(Request::ajax()){
			$gestoresDao = new GestoresDao();
			$gestores = $gestoresDao->getInfoGestorPorId($_POST['id']);
			return Response::json($gestores);
		}
	}
	
	public function nuevoGestor(){
		if(Request::ajax()){
		    $gestoresDao = new GestoresDao();
			$result = $gestoresDao->insertNuevoGestor(Funciones::getPost());
			return Response::json($result);
		}
	}
	
	public function eliminarGestor(){
		if(Request::ajax()){
			$gestoresDao = new GestoresDao();
			$result = $gestoresDao->deleteGestor(Funciones::getPost());
			return Response::json($result);
		}
	}
	
	public function modificarGestor(){
		if(Request::ajax()){
			$gestoresDao = new GestoresDao();
			$result = $gestoresDao->modificarGestor(Funciones::getPost());
			return Response::json($result);
		}
	}
	
	public function getInfoMenuPorId(){
		if(Request::ajax()){
			$menusDao = new MenusDao();
			$post = Funciones::getPost();
			$result = $menusDao->getMenuPorId($post->id);
			return json_encode($result);
		}
	}
	
	public function editarMenu(){
		if(Request::ajax()){
			$menusDao = new MenusDao();
			$info = Funciones::getPost();
			$result = $menusDao->updateMenu($info);
			return Response::json($result);
		}
	}
	
	public function eliminarMenu(){
		if(Request::ajax()){
			$menusDao = new MenusDao();
			$info = Funciones::getPost();
			return Response::json($menusDao->deleteMenuPorId($info->id));
		}
	}
	
	public function crearNouMenu(){
		if(Request::ajax()){
			$menusDao = new MenusDao();
			$aux = new \stdClass();
			$info = Funciones::getPost();

			$aux->id_gestor = $info->id_gestor;
			$aux->nombre = $info->nombre;
			$aux->permiso = $info->permiso;
			
			if(empty($info->ruta)){ 
				$aux->enlace = "NULL";
			}else{
				$aux->enlace = "'".$info->ruta."'";
			}
			
			$aux->parent = 0;
			$aux->icono = $info->icono;
			$aux->orden = "NULL";
			
			$result = $menusDao->crearNuevoMenu($aux);
			return Response::json($result);
		}
	}
	
	public function crearSubMenu(){
		
		if(Request::ajax()){
			$aux = new \stdClass();
			$menusDao = new MenusDao();
			$info = Funciones::getPost();
			
	
			$aux->id_gestor = $info->id_gestor;
			$aux->nombre = $info->nombre;
			$aux->permiso = $info->permiso;
				
			if(empty($info->ruta)){
				$aux->enlace = "NULL";
			}else{
				$aux->enlace = "'".$info->ruta."'";
			}
				
			$aux->parent = $info->parent;
			$aux->icono = "NULL";
			$aux->orden = "NULL";
			$result = $menusDao->crearNuevoMenu($aux);
			return Response::json($result);
		}
	}
	
	public function editarSubmenus(){
	
		$result = '';
		if(Request::ajax()){
			$menusDao = new MenusDao();
			$updateMenus = MenusUtils::cambioOrdenSubMenus($_POST['info']);
			
			try {
				DB::beginTransaction();
				$result = '';
				foreach($updateMenus as $aux){
					$result .= $menusDao->cambioOrdenMenus($aux);
				}
				DB::commit();
			} catch (Exception $e) {
				DB::rollback();
				LogsDao::nuevo("Error".__FUNCTION__,$e->getMessage());
			}
			
			
			if (strpos($result,'0') !== false) {
			    return Response::json(false);
			}else{
			    return Response::json(true);
			}
		}
	}

	function getSubMenusPorId(){
		if(Request::ajax()){
			$menusDao = new MenusDao();
			$subMenus = $menusDao->getSubmenusPerIdMenu($_POST['id']);
			return Response::json($subMenus);
		}
	}
	
	function getInfoSubMenuPorId(){
		if(Request::ajax()){
			$menusDao = new MenusDao();
			$subMenu = $menusDao->getMenuPorId($_POST['id']);
			return json_encode($subMenu);
		}
	}
	
	
}

?>